// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * A visitor to find and register "goog.provide" calls within a given file.
 */


/**
 * A node visitor that registers symbols that a file "goog.provide"s.
 *
 * @constructor
 * @implements {Documenter.Visitor}
 */
var ProvideV = exports.ProvideV = function ProvideV() {
  /**
   * The next visitor in the chain.
   *
   * @type {Documenter.Visitor}
   * @private
   */
  this.nextVisitor_ = null;
};


/**
 * @param {Documenter.Visitor} visitor The next visitor in the chain.
 */
ProvideV.prototype.setNext = function (visitor) {
  this.nextVisitor_ = visitor;
};


/**
 * Determine if a node represents global assignment and, if so, document it.
 *
 * @param {Node} node The node to visit.
 * @parma {DocInfo} docInfo The page's documentation info object.
 * @param {JSDocInfo} jsdoc The jsdoc for the given node or null if no
 *    doc comments were attached.
 */
ProvideV.prototype.visit = function (node, docInfo, jsdoc) {
  if (node.isCall() &&
      node.getFirstChild().getQualifiedName() === 'goog.provide') {

    docInfo.provides.push(node.getChildAtIndex(1).getString());
  }

  if (this.nextVisitor_) {
    this.nextVisitor_.visit(node, docInfo, jsdoc);
  }
};
